#include <pthread\cppthread.h>

cppthread_t::cppthread_t(void*(*threadFunc)(void*), void* args, pthread_attr_t* attrib){
    pthread_create( &thread, attrib, threadFunc, args );
}
cppthread_t::~cppthread_t(){
    pthread_detach(thread);
}

void* cppthread_t::join(){
    void* out;
    pthread_join( thread, &out );
    return out;
}

void cppthread_t::cancel(){
    pthread_cancel(thread);
}
cppthread_mutex_t::cppthread_mutex_t(): mutex( PTHREAD_MUTEX_INITIALIZER ){}
cppthread_mutex_t::~cppthread_mutex_t(){}

void cppthread_mutex_t::lock(){
    pthread_mutex_lock(&mutex);
}
bool cppthread_mutex_t::trylock(){
    return !( pthread_mutex_trylock(&mutex) == EBUSY );
}
bool cppthread_mutex_t::timedlock(const struct timespec * timeout){
    return !( pthread_mutex_timedlock(&mutex, timeout) == ETIMEDOUT );
}
void cppthread_mutex_t::unlock(){
    pthread_mutex_unlock(&mutex);
}

cppthread_cond_t::cppthread_cond_t():   mutex( PTHREAD_MUTEX_INITIALIZER ),
                                        cond( PTHREAD_COND_INITIALIZER ){}
cppthread_cond_t::~cppthread_cond_t(){}

void cppthread_cond_t::wait(){
    pthread_cond_wait(&cond, &mutex);
}
bool cppthread_cond_t::timedwait(const struct timespec * timeout){
    return !(pthread_cond_timedwait(&cond, &mutex, timeout) == ETIMEDOUT);
}
void cppthread_cond_t::signal(){
    pthread_cond_signal(&cond);
}
void cppthread_cond_t::broadcast(){
    pthread_cond_broadcast(&cond);
}
